import Vue from 'vue'
// import store from '@/store';

Vue.prototype.$router={
		push:( option, isAuth = false ) => {
			const app = getApp();
			// let tabRoutes = app.globalData.tabRoutes;
			let tabRoutes = [];//tab页的路由
			// option = { path, params = {}, query, type = '' }
			let url, params, type;
			if(typeof option == 'string'){
				url = option;
				params = {};
				type = 'navigate';
			}
			else{
				url = option.path;
				params = option.params||{};
				type = option.type||'navigate';
			}
			//是否需要判断登录
			if(isAuth){
				if(!store.getters.isLogin){//未登录，替换地址，源地址转换成参数加入params
					params.redirect = url;
					url = '/pages/user/login/index';
				}
			}
			if(option.query){
				config.success = (res) =>{
					res.eventChannel.emit('query', option.query)
				}
			}
			if(url[0]!='/') url='/'+url;
			
			//处理传参
			let paramStr = ''
			for(let k in params){
				paramStr+=`&${k}=${params[k]}`
			}
			
			let config = { url: url+'?'+paramStr.slice(1) };
			
			if(type == 'navigate'){
				if(tabRoutes.includes(url)){
					uni.switchTab(config)
				}
				else{
					uni.navigateTo(config)
				}
				
			}
			if(type == 'redirect'){
				uni.redirectTo(config)
			}
			if(type == 'reLaunch'){
				uni.reLaunch(config)
			}
		},
		back:(delta=1)=>{
			uni.navigateBack({ delta })
		}
	}

